<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>输入提示后查询</title>
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        html, body, #container {
            height: 100%;
            width: 100%;
        }

        .amap-icon img,
        .amap-marker-content img {
            width: 25px;
            height: 34px;
        }

        .marker {
            position: absolute;
            top: -20px;
            right: -118px;
            color: #fff;
            padding: 4px 10px;
            box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
            white-space: nowrap;
            font-size: 12px;
            font-family: "";
            background-color: #25A5F7;
            border-radius: 3px;
        }

        .input-card {
            width: 18rem;
            z-index: 170;
        }

        .input-card .btn {
            margin-right: .8rem;
        }

        .input-card .btn:last-child {
            margin-right: 0;
        }
    </style>
    <style>
        html, body {
            margin: 0;
            height: 100%;
            width: 100%;
            position: absolute;
        }

        #container {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            width: 100%;
            height: 100%;
        }

        .button-group {
            position: absolute;
            bottom: 20px;
            right: 20px;
            font-size: 12px;
            padding: 10px;
        }

        .button-group .button {
            height: 28px;
            line-height: 28px;
            background-color: #0D9BF2;
            color: #FFF;
            border: 0;
            outline: none;
            padding-left: 5px;
            padding-right: 5px;
            border-radius: 3px;
            margin-bottom: 4px;
            cursor: pointer;
        }

        .button-group .inputtext {
            height: 26px;
            line-height: 26px;
            border: 1px;
            outline: none;
            padding-left: 5px;
            padding-right: 5px;
            border-radius: 3px;
            margin-bottom: 4px;
            cursor: pointer;
        }

        /*
       .tip {
           position: absolute;
           bottom: 30px;
           right: 10px;
           background-color: #FFF;
           text-align: center;
           border: 1px solid #ccc;
           line-height: 30px;
           border-radius: 3px;
           padding: 0 5px;
           font-size: 12px;
       }
       */
        #tip {
            background-color: #fff;
            padding-left: 10px;
            padding-right: 10px;
            position: absolute;
            font-size: 12px;
            right: 10px;
            top: 20px;
            border-radius: 3px;
            border: 1px solid #ccc;
            line-height: 30px;
        }

        /*
        #tip input[type='button'] {
            margin-top: 10px;
            margin-bottom: 10px;
            background-color: #0D9BF2;
            height: 30px;
            text-align: center;
            line-height: 30px;
            color: #fff;
            font-size: 12px;
            border-radius: 3px;
            outline: none;
            border: 0;
        }
        */
        .amap-info-content {
            font-size: 12px;
        }

        #myPageTop {
            position: absolute;
            top: 5px;
            right: 10px;
            background: #fff none repeat scroll 0 0;
            border: 1px solid #ccc;
            margin: 10px auto;
            padding: 6px;
            font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";
            font-size: 14px;
        }

        #myPageTop label {
            margin: 0 20px 0 0;
            color: #666666;
            font-weight: normal;
        }

        #myPageTop #tipinput {
            width: 300px;
        }

        #myPageTop .column2 {
            padding-left: 25px;
        }
    </style>
    <script type="text/javascript"
            src="https://webapi.amap.com/maps?v=1.4.15&key=38ec219a062bbf92f1f3ccd9a574752b&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div id="myPageTop">
    <table>
        <tr>
            <td>
                <label>请输入关键字：</label>
                <input id="tipinput"/>
                <input type="button" id="add_address" value="确定添加">
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    //地图加载
    var map = new AMap.Map("container", {
        resizeEnable: true
    });
    //输入提示
    var autoOptions = {
        input: "tipinput"
    };
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发

    //搜索结果
    var address_result = [];

    function select(e) {
        placeSearch.setCity(e.poi.adcode);
        placeSearch.search(e.poi.name, function (status, result) {
            // 查询成功时，result即对应匹配的POI信息
            this.address_result = result;
            console.log(result)
            var pois = result.poiList.pois;
            for (var i = 0; i < pois.length; i++) {
                var poi = pois[i];
                var marker = [];
                marker[i] = new AMap.Marker({
                    position: poi.location,   // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
                    title: poi.name
                });
                map.add(marker[i]);
            }
        });  //关键字查询查询
    }

    $("#add_address").click(function () {
        // console.log(map)
        var val = $("#tipinput").val();
        console.log(val);
        console.log(address_result);
        var pois = address_result.poiList.pois;
        for (var i = 0; i < pois.length; i++) {
            var poi = pois[i];
            var addressName = poi.pname + poi.cityname + poi.adname + poi.address + poi.name;
            if (poi.name == val) {
                var param = {
                    address: addressName,
                    longitude: poi.location.R,
                    latitude: poi.location.Q
                }
                axios.post("/company/addAddress.do", param).then((resp) => {
                    if (resp) {
                        confirm("添加成功!")
                    }
                })
            }
        }
    })
</script>
</body>
</html>